import 'dart:convert';

/// 评标请求参数模型
class ExpertBidRequest {
  /// 专家ID
  final int expertId;
  
  /// 供应商ID
  final int supplierId;
  
  /// 项目ID
  final int projectId;
  
  /// 打分总分
  final int score;
  
  /// 打分表格表头数据
  final List<BidTableHeaderItem> tableHeader;
  
  /// 打分表格数据
  final List<BidTableDataItem> tableData;
  
  /// 是否是组长提交（或最后一位专家提交）：0-否，1-是
  final int isLeader;

  ExpertBidRequest({
    required this.expertId,
    required this.supplierId,
    required this.projectId,
    required this.score,
    required this.tableHeader,
    required this.tableData,
    this.isLeader = 0,
  });

  /// 转换为请求参数Map
  Map<String, dynamic> toQueryParams() {
    return {
      'expertId': expertId.toString(),
      'supplierId': supplierId.toString(),
      'projectId': projectId.toString(),
      'score': score.toString(),
      'tableHeader': _encodeTableHeader(),
      'tableData': _encodeTableData(),
      'isLeader': isLeader.toString(),
    };
  }

  /// 编码表头数据为JSON字符串
  String _encodeTableHeader() {
    final headerList = tableHeader.map((item) => item.toJson()).toList();
    return jsonEncode(headerList);
  }

  /// 编码表格数据为JSON字符串
  String _encodeTableData() {
    final dataList = tableData.map((item) => item.toJson()).toList();
    return jsonEncode(dataList);
  }

  /// 验证总分是否等于各项分数之和
  bool validateScore() {
    final totalScore = tableData.fold<int>(0, (sum, item) => sum + item.score);
    return totalScore == score;
  }

  @override
  String toString() {
    return 'ExpertBidRequest(expertId: $expertId, supplierId: $supplierId, '
        'projectId: $projectId, score: $score, isLeader: $isLeader, '
        'tableHeaderCount: ${tableHeader.length}, tableDataCount: ${tableData.length})';
  }
}

/// 打分表格表头项
class BidTableHeaderItem {
  /// 评分项名称
  final String name;
  
  /// 该项的最高分值
  final int maxScore;

  BidTableHeaderItem({
    required this.name,
    required this.maxScore,
  });

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'maxScore': maxScore,
    };
  }

  factory BidTableHeaderItem.fromJson(Map<String, dynamic> json) {
    return BidTableHeaderItem(
      name: json['name'] as String,
      maxScore: json['maxScore'] as int,
    );
  }

  @override
  String toString() => 'BidTableHeaderItem(name: $name, maxScore: $maxScore)';
}

/// 打分表格数据项
class BidTableDataItem {
  /// 评分子项名称
  final String item;
  
  /// 该项的实际得分
  final int score;
  
  /// 该项的最高分值
  final int maxScore;

  BidTableDataItem({
    required this.item,
    required this.score,
    required this.maxScore,
  });

  Map<String, dynamic> toJson() {
    return {
      'item': item,
      'score': score,
      'maxScore': maxScore,
    };
  }

  factory BidTableDataItem.fromJson(Map<String, dynamic> json) {
    return BidTableDataItem(
      item: json['item'] as String,
      score: json['score'] as int,
      maxScore: json['maxScore'] as int,
    );
  }

  @override
  String toString() => 'BidTableDataItem(item: $item, score: $score, maxScore: $maxScore)';
}
